package syf.stack;

public class SinglelistStack {
    public static void main(String[] args) {
        ListStack listStack=new ListStack(new Node());
        Node node=new Node(1);
        Node node1=new Node(2);
        Node node2=new Node(3);
        listStack.push(node);
        listStack.push(node1);
        listStack.push(node2);
        listStack.show();
        listStack.pop();
        listStack.show();
        listStack.pop();listStack.pop();listStack.pop();
    }
}
class ListStack{
    private Node head;

    public ListStack(Node head) {
        this.head = head;
    }

    //入栈
    public void push(Node node){
        Node curr=head;
        while (true){
            if (curr.getNext()==null)
                break;
            curr=curr.getNext();
        }
        curr.setNext(node);
        System.out.println("添加节点"+curr.getNext().getNo());
    }
    //出栈
    public void pop(){
        Node curr=head;
        if (curr.getNext()==null){
            System.out.println("没有节点了");
            return;}
        while (true){
            if (curr.getNext().getNext()==null) {
                System.out.println("弹出节点"+curr.getNext().getNo());
                curr.setNext(null);
                break;
            }
            curr=curr.getNext();
        }
    }
    //遍历 单向 转置
    public void show(){
        Node curr=head;
        StringBuilder x= new StringBuilder();
        if (curr.getNext()==null)
            return;
        while (true){
            if (curr.getNext()==null)
                break;
            x.insert(0, curr.getNext().getNo()+"\n");
            curr=curr.getNext();
        }
        System.out.println(x);
    }
}
//链表的节点
class Node{
    private Node next;
    private int no;

    public Node getNext() {
        return next;
    }

    public void setNext(Node next) {
        this.next = next;
    }

    public int getNo() {
        return no;
    }

    public void setNo(int no) {
        this.no = no;
    }

    public Node() {
    }

    public Node(int no) {
        this.no = no;
    }
}